const SubtitleService = require('./subtitleService');

class MediaService {
  constructor() {
    this.subtitleService = new SubtitleService();
    this.currentMedia = null;
    this.isPlaying = false;
    this.currentTime = 0;
  }

  /**
   * Load media with subtitle file
   * @param {Object} mediaData - Media data containing file paths
   * @returns {Promise} - Promise that resolves when media is loaded
   */
  async loadMedia(mediaData) {
    try {
      this.currentMedia = {
        ...mediaData,
        subtitles: null,
        lyrics: null,
      };

      // Load subtitles if available
      if (mediaData.subtitle_file && mediaData.subtitle_type) {
        switch (mediaData.subtitle_type.toLowerCase()) {
          case 'srt':
            this.currentMedia.subtitles = await this.subtitleService.parseSRT(mediaData.subtitle_file);
            break;
          case 'ass':
            this.currentMedia.subtitles = await this.subtitleService.parseASS(mediaData.subtitle_file);
            break;
          default:
            throw new Error(`Unsupported subtitle type: ${mediaData.subtitle_type}`);
        }
      }

      // Load lyrics if available
      if (mediaData.lyrics_file && mediaData.lyrics_file.endsWith('.lrc')) {
        this.currentMedia.lyrics = await this.subtitleService.parseLRC(mediaData.lyrics_file);
      }

      return { success: true, data: this.currentMedia };
    } catch (error) {
      throw new Error(`Failed to load media: ${error.message}`);
    }
  }

  /**
   * Get subtitle text at specific time
   * @param {number} time - Time in seconds
   * @returns {Object|null} - Subtitle data or null if not found
   */
  getSubtitleAtTime(time) {
    if (!this.currentMedia || !this.currentMedia.subtitles) {
      return null;
    }

    return this.subtitleService.findSubtitleAtTime(this.currentMedia.subtitles, time);
  }

  /**
   * Get lyrics text at specific time
   * @param {number} time - Time in seconds
   * @returns {Object|null} - Lyrics data or null if not found
   */
  getLyricsAtTime(time) {
    if (!this.currentMedia || !this.currentMedia.lyrics) {
      return null;
    }

    return this.subtitleService.findLyricsAtTime(this.currentMedia.lyrics, time);
  }

  /**
   * Extract words from subtitle at specific time
   * @param {number} time - Time in seconds
   * @returns {Array} - Array of words
   */
  getWordsAtTime(time) {
    const subtitle = this.getSubtitleAtTime(time);
    if (!subtitle) {
      return [];
    }

    return this.subtitleService.extractWords(subtitle.text);
  }

  /**
   * Play media
   * @returns {Object} - Play status
   */
  play() {
    this.isPlaying = true;
    return { success: true, message: 'Media playing' };
  }

  /**
   * Pause media
   * @returns {Object} - Pause status
   */
  pause() {
    this.isPlaying = false;
    return { success: true, message: 'Media paused' };
  }

  /**
   * Stop media
   * @returns {Object} - Stop status
   */
  stop() {
    this.isPlaying = false;
    this.currentTime = 0;
    return { success: true, message: 'Media stopped' };
  }

  /**
   * Seek to specific time
   * @param {number} time - Time in seconds
   * @returns {Object} - Seek status
   */
  seek(time) {
    this.currentTime = time;
    return { success: true, message: `Seeked to ${time} seconds` };
  }
}

module.exports = MediaService;
